package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.manage.config.Config;
import com.manage.model.SpCode;
import com.manage.model.SpCodeProvince;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class SpCodeServiceImpl extends AbstractService implements SpCodeService {
	private Logger logger;
	public SpCodeServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
	@Override
	public SpCode getSpCodeById(int id) {
		String sql = "select * from spcode where id="+id;
		ModelGenerator<SpCode> dbcpmg = new ModelGenerator<SpCode>(SpCode.class);
		return dbcpmg.generate(dbcp, sql); 
	}

	@Override
	public List<SpCode> getSpCodes() {
		String sql = "select * from spcode order by createdate desc";
		ModelGenerator<SpCode> dbcpmg = new ModelGenerator<SpCode>(SpCode.class);
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public List<SpCode> getSpCodes(int spId, String codeType) {
		String sql = "select * from spcode where spid="+spId;
		if(codeType!=null && !codeType.equals(""))
			sql += " and codeType='"+codeType+"'";
		sql += " order by createdate desc";
		ModelGenerator<SpCode> dbcpmg = new ModelGenerator<SpCode>(SpCode.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	@Override
	public List<SpCode> getSpCodesByStatus(int spId, String codeType) {
		String sql = "select * from spcode where spid="+spId;
		if(codeType!=null && !codeType.equals(""))
			sql += " and codeType='"+codeType+"'";
		sql += " and codestatus='0' order by createdate desc";
		ModelGenerator<SpCode> dbcpmg = new ModelGenerator<SpCode>(SpCode.class);
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public boolean saveSpCode(String type, SpCode spCode,List<SpCodeProvince> list) {
		String [] sql = null;
		if(type.equalsIgnoreCase("insert"))
		{
			sql = new String [list.size()+1];
			spCode.setId(getTableId("spcode"));

			sql[0] = "insert into spcode(id,spid,codename,carrier,ordertype,codetype," +
					"smspayport,smspaycode,basepayaction,codedesc,codestatus,level,createuser,createdate,openprovince" +
					",codefeevalue,codedailylimit,codemonthlylimit,codedailycapacity,codemonthlycapacity,codeua,space," +
					" balancetype,balancevalue,balancecyc,startdate,expireddate,baseparameter) " +
					"values ("+spCode.getId()+","+spCode.getSpId()+",'"+spCode.getCodeName()+
					"','"+spCode.getCarrier()+"','"+spCode.getOrderType()+"','"+spCode.getCodeType()+"'" +
					",'"+spCode.getSmspayport()+
					"','"+encodeDb(spCode.getSmspaycode())+"','"+spCode.getBasepayaction()+"','"+spCode.getCodeDesc()+"','"+spCode.getCodeStatus()+
					"',"+spCode.getLevel()+",'"+spCode.getCreateUser()+"','"+spCode.getCreateDate()+"','"+spCode.getOpenProvince()+"'" +
					",'"+spCode.getCodefeevalue()+"','"+spCode.getCodedailylimit()+"','"+spCode.getCodemonthlylimit()+"'" +
					",'"+spCode.getCodedailycapacity()+"','"+spCode.getCodemonthlycapacity()+"','"+spCode.getCodeua()+"','"+spCode.getSpace()+
					"','"+spCode.getBalancetype()+"','"+spCode.getBalancevalue()+"','"+spCode.getBalancecyc()+
					"','"+spCode.getStartdate()+"','"+spCode.getExpireddate()+"','"+spCode.getBaseparameter()+"')";
			for(int i =0 ;i<list.size();i++)
			{
				int id = getTableId("spcodeprovince");
				SpCodeProvince p = list.get(i);
				sql[i+1] = "insert into spcodeprovince(id,spid,spcodeid," +
						"provincecode,shieldregion,feevalue,dailylimit," +
						"monthlylimit,dailycapacity,monthlycapacity," +
						"createdate,createuser,codestatus,pbalancetype,pbalancevalue,pbalancecyc) values ("+(id)+","+spCode.getSpId()+","+spCode.getId()+"" +
						",'"+p.getProvinceCode()+"','"+p.getShieldRegion()+"','"+p.getFeeValue()+"'" +
						",'"+p.getDailyLimit()+"','"+p.getMonthlyLimit()+"','"+p.getDailyCapacity()+"'" +
						",'"+p.getMonthlyCapacity()+"','"+spCode.getCreateDate()+"','"+spCode.getCreateUser()+"','"+spCode.getCodeStatus()+"'" +
						",'"+p.getPbalancetype()+"','"+p.getPbalancevalue()+"','"+p.getPbalancecyc()+"')";
			}
		}else if (type.equalsIgnoreCase("update"))
		{
			
			sql = new String [list.size()+2];
			sql[0]="update spcode set codename='"+spCode.getCodeName()+"'" +
					",codename='"+spCode.getCodeName()+"'" +
					",carrier='"+spCode.getCarrier()+"'" +
					",ordertype='"+spCode.getOrderType()+"'" +
					",codetype='"+spCode.getCodeType()+"'" +
					",smspayport='"+spCode.getSmspayport()+"'" +
					",smspaycode='"+encodeDb(spCode.getSmspaycode())+"'" +
					",basepayaction='"+spCode.getBasepayaction()+"'" +
					",codedesc='"+spCode.getCodeDesc()+"'" +
					",level='"+spCode.getLevel()+"'" +
					",codestatus='"+spCode.getCodeStatus()+"'" +
					",openprovince='"+spCode.getOpenProvince()+"'" +
					",codefeevalue='"+spCode.getCodefeevalue()+"'" +
					",codedailylimit='"+spCode.getCodedailylimit()+"'" +
					",codemonthlylimit='"+spCode.getCodemonthlylimit()+"'" +
					",codedailycapacity='"+spCode.getCodedailycapacity()+"'" +
					",codemonthlycapacity='"+spCode.getCodemonthlycapacity()+"'" +
					",codeua='"+spCode.getCodeua()+"'" +
					",space='"+spCode.getSpace()+"'" +
					",balancetype='"+spCode.getBalancetype()+"'" +
					",balancevalue='"+spCode.getBalancevalue()+"'" +
					",balancecyc='"+spCode.getBalancecyc()+"'" +
					",startdate='"+spCode.getStartdate()+"'" +
					",expireddate='"+spCode.getExpireddate()+"'" +
					",baseparameter='"+spCode.getBaseparameter()+"'" +
					" where id ="+spCode.getId();
			//2013 -04 -11 添加
			//codefeevalue,codedailylimit,codemonthlylimit,codedailycapacity,codemonthlycapacity,codeua
			//balancetype,balancevalue,balancecyc,startdate,expireddate
			
			sql[1] = "delete from spcodeprovince where spcodeid="+spCode.getId();
			
			for(int i =0 ;i<list.size();i++)
			{
				int id = getTableId("spcodeprovince");
				SpCodeProvince p = list.get(i);
				sql[i+2] = "insert into spcodeprovince(id,spid,spcodeid," +
						"provincecode,shieldregion,feevalue,dailylimit," +
						"monthlylimit,dailycapacity,monthlycapacity," +
						"createdate,createuser,codestatus,pbalancetype,pbalancevalue,pbalancecyc) values ("+(id)+","+spCode.getSpId()+","+spCode.getId()+"" +
						",'"+p.getProvinceCode()+"','"+p.getShieldRegion()+"','"+p.getFeeValue()+"'" +
						",'"+p.getDailyLimit()+"','"+p.getMonthlyLimit()+"','"+p.getDailyCapacity()+"'" +
						",'"+p.getMonthlyCapacity()+"','"+spCode.getCreateDate()+"','"+spCode.getCreateUser()+"','"+spCode.getCodeStatus()+"'" +
						",'"+p.getPbalancetype()+"','"+p.getPbalancevalue()+"','"+p.getPbalancecyc()+"')";
			}
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql = new String [2];
			if(spCode.getCodeStatus().equals("0"))
				spCode.setCodeStatus("1");
			else
				spCode.setCodeStatus("0");
			sql[0]="update spcode set codestatus='"+spCode.getCodeStatus()+"' where id ="+spCode.getId();
			sql[1]= "update spcodeprovince set codestatus='"+spCode.getCodeStatus()+"' where spcodeid="+spCode.getId();
		}
		else if (type.equalsIgnoreCase("online"))
		{
			sql = new String [2];
			sql[0]="update spcode set codestatus='0' where id ="+spCode.getId();
			sql[1]= "update spcodeprovince set codestatus='0' where spcodeid="+spCode.getId();
		}
		else if (type.equalsIgnoreCase("offline"))
		{
			sql = new String [2];
			sql[0]="update spcode set codestatus='1' where id ="+spCode.getId();
			sql[1]= "update spcodeprovince set codestatus='1' where spcodeid="+spCode.getId();
		}
		else if (type.equalsIgnoreCase("test"))
		{
			sql = new String [2];
			sql[0]="update spcode set codestatus='2' where id ="+spCode.getId();
			sql[1]= "update spcodeprovince set codestatus='2' where spcodeid="+spCode.getId();
		}
		else if (type.equalsIgnoreCase("pause"))
		{
			sql = new String [2];
			sql[0]="update spcode set codestatus='2' where id ="+spCode.getId();
			sql[1]= "update spcodeprovince set codestatus='2' where spcodeid="+spCode.getId();
		}
		else if (type.equalsIgnoreCase("restore"))
		{
			sql = new String [2];
			if(spCode.getCodeStatus().equals("0"))
				spCode.setCodeStatus("1");
			else
				spCode.setCodeStatus("0");
			sql[0]="update spcode set codestatus='"+spCode.getCodeStatus()+"' where id ="+spCode.getId();
			sql[1]= "update spcodeprovince set codestatus='"+spCode.getCodeStatus()+"' where spcodeid="+spCode.getId();
		}
		logger.info("[sql] [method:saveSpCode] ["+sql+"]");
		return ConnectionManager.getInstance().executeBatch(dbcp, sql);
	}
	@Override
	public List<SpCodeProvince> getSpCodeProvinces(int spCodeId) {
		String sql = "select * from spcodeprovince where spcodeid="+spCodeId+" order by createdate";
		ModelGenerator<SpCodeProvince> dbcpmg = new ModelGenerator<SpCodeProvince>(SpCodeProvince.class);
		return dbcpmg.generateList(dbcp, sql);
	}


}
 